<?php

namespace app\controller\api\info;

use app\BaseController;
use app\common\repositories\info\OrderRepository as repository;
use app\common\repositories\info\SizeRepository;
use app\common\repositories\system\AdminRepository;
use think\App;
use think\exception\ValidateException;

class Order extends BaseController
{
    protected repository $repository;

    public function __construct(App $app, repository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
    }


    public function check()
    {
        $order = $this->repository->getWhere(['is_del'=>0,'order_sn'=>input('order_sn')]);
        if(!$order){
            return fail('订单不存在');
        }

        if($order->status != 0){
            return fail('当前订单已提交');
        }

        return success($order);
    }


    public function lst()
    {
        [$page,$limit] = $this->request->page();
        $where['uid'] = request()->uid();
        return success($this->repository->getList($where,$page,$limit));
    }


    public function detail($id)
    {
        $uid = request()->uid();
        $order = $this->repository->getWhere(['is_del'=>0,'order_id'=>$id,'uid'=>$uid]);
        if(!$order)
            throw  new ValidateException('订单不存在');
        $order->append(['shopName']);
        return success($order);
    }



    public function create()
    {
        $uid = request()->uid();
        $params = $this->request->params([
            'order_sn','images','name','height','width','material','print_style','size_name','ext','admin_id'
        ]);

        validate([
            'order_sn|订单号'=>'require',
            'height|高度'=>'require',
            'width|宽度'=>'require',
            //'material|材质'=>'require',
            'print_style|印刷方式'=>'require',
            'size_name|尺寸'=>'require',
            'name|昵称'=>'require',
            'images|图片'=>'require',
            'ext|类型'=>'require|array'
        ])->check($params);

        $order = $this->repository->getWhere(['is_del'=>0,'order_sn'=>$params['order_sn']]);
//        if(!$order){
//            throw new ValidateException('订单不存在');
//        }

        // -1 无效 1已上传  2已完成
        if(!$params['admin_id']){
            throw new ValidateException('请选择店铺');
        }

        if($order){
            throw new ValidateException('当前订单已提交');
        }

        $count = 0;
        foreach ($params['images'] as $key=> $img){
            if(!isset($img['count']) || !$img['count']){
                throw new ValidateException('第'.($key+1).'组数量不能为空');
            }

            $count += $img['count'];
            // 正 img['z'] 反 img['f']
        }


        $size = app()->make(SizeRepository::class)->getWhere(['name'=>$params['size_name']]);
        if(!$size){
            throw new ValidateException('尺寸不存在');
        }


        $params['count'] = $count;


        foreach ($params['ext'] as $i){
            if(!isset($i['title']) || !$i['title'])
                throw new ValidateException("类型有误");

            if(!isset($i['value']) || !$i['value']){
                throw new ValidateException('请选择'.$i['title']);
            }
        }

        $params['ext'] = json_encode($params['ext']);

        // todo 判断数量

//        if($count != $size->count){
//            throw new ValidateException('请上传'.$size->count.'张图片');
//        }

        $params['images'] = json_encode($params['images']);
        $params['uid'] = $uid;

        $params['status'] = mb_strlen($params['order_sn']) >= 10 ? 1 : -1;
        $this->repository->create($params);
        return success('提交成功');
    }



    public function config()
    {
        $size = app()->make(SizeRepository::class)->getSearch()->where('delete_time',0)->select();
        foreach ($size as &$item){
            $item['rate'] = (float)bcdiv($item['width'],$item['height'],2);
        }

        $printStyle = systemConfig('sys_print_style_lst') ? explode(',',systemConfig('sys_print_style_lst')) : [];
        $ext = json_decode(systemConfig('sys_material_lst'),true);

        return success(compact('size','printStyle','ext'));
    }


    public function shopLst()
    {
        $list = app()->make(AdminRepository::class)->getSearch()->where('admin_id','<>',1)
            ->field('real_name,admin_id')->where('is_del',0)->select();

        return success($list);
    }

}